/**
 * Copyright (c) 2012, Hakans Pirates - 
 * Johan Andersson spaiki17@gmail.com, 
 * Mattias Lundberg lundberg.mattias@gmail.com, 
 * Samuel Sjodin sjoodin@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pirates.notifications;

import com.jme3.app.SimpleApplication;
import com.jme3.audio.AudioNode;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.math.ColorRGBA;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JOptionPane;
import pirates.Settings;

/**
 * 
 * @author mattias
 */
public class Notify {

	private static BitmapText guiText;

	public static void popup(String message) {
		JOptionPane.showMessageDialog(null, message);
	}

	public static void stdout(String message) {
		System.out.println(message);
	}

	public static void debug(String message) {
		if (Settings.DEBUG) {
			stdout(message);
		}
	}

	public static void except(Exception e) {
		if (Settings.DEBUG) {
			e.printStackTrace();
		}
	}
	private static BitmapText debugText;
	private static Map<String, Object> valueMap = new HashMap<String, Object>(
			10);

	public static void displayValueDebug(SimpleApplication app, String key,
			Object o) {
		if (Settings.DEBUG) {
			if (debugText == null) {
				initDisplayDebug(app);
			}

			valueMap.put(key, o);

			debugText.setText("");
			String text = "";
			for (Entry<String, Object> e : valueMap.entrySet()) {
				text += e.getKey() + " " + e.getValue() + "\n";
			}
			debugText.setText(text);

		}
	}

	private static void initDisplayDebug(SimpleApplication app) {
		BitmapFont guiFont = app.getAssetManager().loadFont(
				"Interface/Fonts/Default.fnt");
		debugText = new BitmapText(guiFont, false);
		debugText.setSize(guiFont.getCharSet().getRenderedSize()); // font size
		debugText.setColor(ColorRGBA.Black); // font color
		// the text
		debugText.setLocalTranslation(0, app.getCamera().getHeight(), 0); // position
		app.getGuiNode().attachChild(debugText);
	}

	public static void playerInformation(SimpleApplication app, float hp,
			float maxHP, int ammo, int maxAmmo, String team) {
		if (guiText == null) {
			initPlayerInformation(app);
		}

		String text = "";

		text += team + " - ";
		text += "HP: " + (int) hp + "/" + (int) maxHP + " - ";
		text += "Ammo: " + ammo + "/" + maxAmmo + "";

		guiText.setText(text);
	}

	public static void gameEndInformation(SimpleApplication app,
			String playersTeam) {
		if (guiText == null) {
			initPlayerInformation(app);
		}

		AudioNode sound;
		String text;
		stdout("player's team: " + playersTeam);
		stdout("winning team: " + Settings.winningTeam);
		if (playersTeam.equals(Settings.winningTeam.toString())) {
			text = "-----  YOU WON!  ------";
			sound = new AudioNode(app.getAssetManager(),
					"Sounds/Effects/victory.ogg");
		} else {
			text = "-----  YOU LOST  ------";
			sound = new AudioNode(app.getAssetManager(),
					"Sounds/Effects/lose.ogg");
		}

		sound.setLooping(false);

		app.getAudioRenderer().playSource(sound);

		guiText.setText(text);
	}

	private static void initPlayerInformation(SimpleApplication app) {
		BitmapFont guiFont = app.getAssetManager().loadFont(
				"Interface/Fonts/Default.fnt");
		guiText = new BitmapText(guiFont, false);
		guiText.setSize(guiFont.getCharSet().getRenderedSize()); // font size
		guiText.setColor(ColorRGBA.Black); // font color
		// the text
		guiText.setLocalTranslation(app.getCamera().getWidth() / 3, app
				.getCamera().getHeight(), 0); // position
		app.getGuiNode().attachChild(guiText);
	}
}
